/* 地图的变量 */
var map = null
/* 光谷数字化地图的变量 */
var docLayer = null
/* 添加画布 */
var source = null
var layer = null
/* 画笔 */
var draw = null
export const initMap = ({ arr, attr, service, row, status_color }) => {
  map = new ol.Map({
    target: "map",
    layers: [TianDiMap_img, TianDiMap_cia, TianDiMap_vec, TiandiMap_cva],
    view: new ol.View({
      center: [114.38, 30.49],
      zoom: 13,
      projection: "EPSG:4326",
    }),
  })
  docLayer = new Zondy.Map.Doc("", "traffic", {
    ip: "localhost",
    port: 6163,
  })
  map.addLayer(docLayer)
  source = new ol.source.Vector({})
  layer = new ol.layer.Vector({
    source,
    style: new ol.style.Style({
      image: new ol.style.Circle({
        radius: 10,
        fill: new ol.style.Fill({
          color: "rgba(255,255,255,0.5)",
        }),
        stroke: new ol.style.Stroke({
          color: "#1fffff",
          width: 2,
        }),
      }),
    }),
  })
  map.addLayer(layer)

  /* 点击图表查询点并高亮的函数 */
  function queryEventPoint(row) {
    // console.log("点击了")
    //console.log(row)
    /* 此时已经能够拿到点击的点的id了 */
    let { id, lnglat } = row
    let queryArr = lnglat.split(",")
    /* 将数组将里边的元素转成数字型 */
    queryArr = queryArr.map(Number)
    /* 然后弄一个flyto,并且高亮 */
    //* 改变封装的方式
    Query.queryByAttr({
      where: `事件编号='${id}'`,
      service: {
        name: "traffic",
        layerId: 2,
      },
      callback: (res) => {
        source.clear()
        //console.log(res)
        var format = new Zondy.Format.PolygonJSON()
        var features = format.read(res)
        source.addFeatures(features)
        attr = [{ type: "int", key: "处理状态", value: 0 }]
        /* 改他的属性和颜色 */

        /* 飞到那里 */
        const view = map.getView()
        view.animate({
          center: queryArr,
          zoom: 14,
          duration: 1000,
        })
      },
    })
  }

  /* 写更新事件状态的函数 */
  function updateEventPoint(row, color) {
    let { id, lnglat } = row
    let status_color = 4
    console.log(color)
    let queryArr = lnglat.split(",")
    /* 将数组将里边的元素转成数字型 */
    queryArr = queryArr.map(Number)
    /* 然后弄一个flyto,并且高亮 */
    //* 改变封装的方式
    Query.queryByAttr({
      where: `事件编号='${id}'`,
      service: {
        name: "traffic",
        layerId: 2,
      },
      callback: (res) => {
        source.clear()
        var format = new Zondy.Format.PolygonJSON()
        var features = format.read(res)
        source.addFeatures(features)
        res = JSON.parse(JSON.stringify(res)).SFEleArray
        /* 这里能够拿到点击的点的fid */
        console.log(res[0].FID)
        let eventFid = res[0].FID
        var attr = []
        if (color == "yellow") {
          status_color = 4
          attr = [{ type: "int", key: "处理状态", value: 1 }]
        } else if (color == "green") {
          status_color = 90
          attr = [{ type: "int", key: "处理状态", value: 2 }]
        }
        Point.updateEvent({
          fid: eventFid,
          position: queryArr,
          attr,
          service: {
            name: "traffic",
            layerId: 2,
          },
          docName: docLayer,
          status_color: status_color,
        })
        /* 改这个点的信息 */
        /* 飞到那里 */
        const view = map.getView()
        view.animate({
          center: queryArr,
          zoom: 14,
          duration: 1000,
        })
      },
    })
  }
  return { queryEventPoint, updateEventPoint }
}
